package java2020spring;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class NormalCalculator extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4575356154177124099L;
	
	
	JPanel panel;
	JPanel panel2;
	JPanel top;
	JTextField text;
	String []key={"sqrt","*","7","8","9","/","4","5","6","+","1","2","3","-",".","0","=","1/x"};
	String []command={"C","back"};
	JButton []keys=new JButton[key.length];
	JButton []commands=new JButton[command.length];
	double result=0.0;
	String count="=";
	boolean isCountValid=true;
	boolean firstNumber=true;
	
	NormalCalculator(){
		setTitle("计算器");
		this.setLayout(new BorderLayout());
		this.setTitle("标准型");
		this.setBounds(300, 100, 400, 400);
		this.setResizable(false);
		
		JMenuBar menuBar= new JMenuBar();
		setJMenuBar(menuBar);
		JMenu menu1=new JMenu("查看(V)");
		JMenu menu2=new JMenu("帮助(H)");
		JMenuItem useHelp=new JMenuItem();
		JMenuItem viewType=new JMenuItem();

		menuBar.add(menu1);
		menuBar.add(menu2);
		JMenuItem menuItem1=new JMenuItem("标准型");
		JMenuItem menuItem2=new JMenuItem("科学型");
		JMenuItem menuItem3=new JMenuItem("进制运算");
		menu1.add(menuItem1);
		menu1.add(menuItem2);
		menu1.add(menuItem3);
		
		menuItem2.addActionListener(new ActionListener(){    //切换到其他计算器

			@Override
			public void actionPerformed(ActionEvent arg0) {
				closeThis();
				ScienceCalculator sci=new ScienceCalculator();
				sci.setVisible(true);
				sci.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
			
		});
		menuItem3.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成的方法存根
				closeThis();
				HexadecimalCalculator hex=new HexadecimalCalculator();
				hex.setVisible(true);
				hex.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
			
		});
		menu2.addMouseListener(new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO 自动生成的方法存根
				HelpWindow helpWindow=new HelpWindow();
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO 自动生成的方法存根
				
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO 自动生成的方法存根
				
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO 自动生成的方法存根
				
			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO 自动生成的方法存根
				
			}

		});
		useHelp.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent arg0) {
				// TODO 自动生成的方法存根
				HelpWindow helpWindow=new HelpWindow(); //创建帮助窗口
				
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO 自动生成的方法存根
				
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO 自动生成的方法存根
				
			}
			
			
		});
		menu1.add(viewType);
		menu2.add(useHelp);
		
		text=new JTextField("0.0");
		text.setHorizontalAlignment(JTextField.RIGHT);
		text.setEditable(false);
		panel=new JPanel();
		panel.setLayout(new GridLayout(5,4,3,3));
		
		for(int i=0;i<commands.length;i++){
			commands[i]=new JButton(command[i]);
			panel.add(commands[i]);
		}
		for(int i=0;i<keys.length;i++){
			keys[i]=new JButton(key[i]);
			panel.add(keys[i]);
		}
		
		panel2=new JPanel();
		panel2.setLayout(new BorderLayout(3,3));
		panel2.add("Center",panel);
		top=new JPanel();
		top.setLayout(new BorderLayout());
		top.add("Center",text);
		getContentPane().setLayout(new BorderLayout(3,5));
		getContentPane().add("North",top);
		getContentPane().add("Center",panel2);
		
		for(int i=0;i<key.length;i++){
			keys[i].addActionListener(this);
		}
		for(int i=0;i<command.length;i++){
			commands[i].addActionListener(this);
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {   //判断事件来源来决定做什么操作
		// TODO 自动生成的方法存根
		String eCmd=e.getActionCommand();
		if("0123456789.".indexOf(eCmd)>=0)
			setTextNumber(eCmd); 
		else if(eCmd.equals(command[0]))
			setTextClear();
		else if(eCmd.equals(command[1]))
			setTextBack();
		else 
			Count(eCmd);
	}
	
	
	public void setTextNumber(String Number){  //点击数字按钮可在文本框显示的函数
		if(firstNumber)
			text.setText(Number);
		else if((Number.equals("."))&&(text.getText().indexOf(".")<0))
			text.setText(text.getText()+".");	
		else if(!Number.equals("."))
			text.setText(text.getText()+Number);
		firstNumber=false;
	}
	
	public void setTextClear(){						 //清空函数
		text.setText("0");
		firstNumber=true;
		result=0.0;
	}
	public void setTextBack(){ 						//退格函数
		String backText=text.getText();
		int i=backText.length();
		if(i>0){
			backText=backText.substring(0,i-1);
			if(backText.length()==0){
				text.setText("0");
				firstNumber=true;
				count="=";
			}
			else
				text.setText(backText);
		}
	}
	public void Count(String countKey){        //根据事件源的计算符号来判断做什么操作
		if(count.equals("+")){
			try{
				result+=Double.valueOf(text.getText()).doubleValue();
			}
			catch(Exception e){}
			
		}
		if(count.equals("-")){
			try{
				result-=Double.valueOf(text.getText()).doubleValue();
			}
			catch(Exception e){}
			
		}
		if(count.equals("*")){
			try{
				result*=Double.valueOf(text.getText()).doubleValue();
			}
			catch(Exception e){}
			
		}
		if(count.equals("/")){
			if(Double.valueOf(text.getText()).doubleValue()==0.0){
				text.setText("除数不能为0");
				isCountValid=false;
			}
			else{
			    try{
			    	result/=Double.valueOf(text.getText()).doubleValue();
			    }
				catch(Exception e){}
			}
		}	
		if(count.equals("1/x")){
			if(result==0.0){
				text.setText("0没有倒数");
				isCountValid=false;
			}
			else
				result=1.0/result;
		}
		if(count.equals("sqrt")){
			if(result<0.0){
				text.setText("0不能开根号");
				isCountValid=false;
			}
			else
				result=Math.sqrt(result);
			}
		if(count.equals("=")){
			 try{
			    	result=Double.valueOf(text.getText()).doubleValue();
			    }
				catch(Exception e){}
		}
			
		if(isCountValid){

				text.setText(String.valueOf(result));
		}
		count=countKey;
		firstNumber=true;
		isCountValid=true;
	}
	public void closeThis(){				//关闭当前窗口
		this.setVisible(false);
	}
	
}
